<?php
include ("simple_html_dom.php");
function jt_decode($str)
{
	//$str= str_replace(array("&nbsp;","<br>"),"",$str);
	$html = str_get_html($str);
	$paxname =array();
	$mangxx=array();
	$mangxx['pnr']=TrimStr($html->find('span[id=lblRefNumber]',0)->plaintext);
	foreach($html->find('tr.Step4_PaxRow') as $element)
				$paxname[]=strtoupper(TrimStr($element->find('td', 1)->plaintext));
	$mangxx['amount']= TrimStr(str_replace(",","",$html->find('span[id=lblTotalFares]', 0)->plaintext));
	$mangxx['paxname']=$paxname;
	$cb = array();
	$dem=0;
	foreach($html->find('tr.Step4ItinRow') as $element)
		{	
			$element->find('div.carrierLogo',0)->innertext='';
			$element->find('div[style=clear: both;]',0)->innertext='';
			$cb[$dem]['sohieu']		=	TrimStr($element->find('td', 0)->plaintext);
			$temp									= $element->find('span.DayString',0)->plaintext;
			if (strlen($temp)<10)
			{
				$temp									=	TrimStr($element->find('td', 1)->plaintext);
				$cb[$dem]['ngaybay']	=	xyly_ngaybay(substr($temp,-6));
			 }	
			else
				$cb[$dem]['ngaybay']	= date("d/m/Y",strtotime($temp));
			$temel=TrimStr($element->find('td', 1)->plaintext);
			$cb[$dem]['sbdi']			=	substr($temel,strrpos($temel,")")+1,5)."&nbsp;".substr($temel,strrpos($temel,"(")+1,3);
			$temel=TrimStr($element->find('td', 2)->plaintext);
			$cb[$dem]['sbden']			=	substr($temel,strrpos($temel,")")+1,5)."&nbsp;".substr($temel,strrpos($temel,"(")+1,3);
			$dem++;
		}
	//exit;	
	$mangxx['flight']=$cb;
	return $mangxx;
}
function xyly_ngaybay($xx)
{
	$ngaythang=array('Jan'=>'01',
								 'Feb'=>'02',
								 'Mar'=>'03',
								 'Apr'=>'04',
								 'May'=>'05',
								 'Jun'=>'06',
								 'Jul'=>'07',
								 'Aug'=>'08',
								 'Sep'=>'09',
								 'Oct'=>'10',
								 'Nov'=>'11',
								 'Dec'=>'12');
	$daten = intval(Date("dm"));
	$datebay =intval(substr($xx,0,2).$ngaythang[substr($xx,-3)]);
	$y = intval(Date("Y"));
	if ($daten > $datebay)
	$y++;
	return date("d/m/Y",strtotime($y."/".$ngaythang[substr($xx,-3)]."/".substr($xx,0,2)));
}

function TrimStr($str)
{
    $str        = trim($str);
    $ret_str    = "";
    for($i=0;$i < strlen($str);$i++)
    {

        if(substr($str, $i, 1) != " ")
        {

            $ret_str .= trim(substr($str, $i, 1));

        }
        else
        {
            while(substr($str,$i,1) == " ")

            {
                $i++;
            }
            $ret_str.= " ";
            $i--; // ***
        }
    }
    return $ret_str;
}